package com.com.util;

import java.util.Arrays;

/**
 * Created by User on 21/08/2015.
 */
public class MathFunction {
    public static void main(String[] args)
    {
        double value = 3.99;
        System.out.println("FloorValue: " + Math.floor(value));
        System.out.println("Ceil Value: " + Math.ceil(value));

        double d[] = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0};
        System.out.println("Median: "+ median(d));
    }

    public static double average(double ds[])
    {
        double avg = 0, sum = 0;
        for(double d : ds)
        {
            sum += d;
        }
        avg = sum / ds.length;
        return avg;
    }

    public static double mean(double ds[])
    {
        return average(ds);
    }

    public static double median(double ds[])
    {
        Arrays.sort(ds);
        int size = ds.length;
        if((size % 2) != 0)
        {
            int index = (size + 1)/2;
            return ds[index-1];
        }

        int index1 = (size/2)-1;
        int index2 = index1+1;
        double median = (ds[index1] + ds[index2])/2;
        return median;
    }
}
